package com.COMP3111H.project;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class My_Profile extends TabActivity {
	final int HELP = 0;
	private TabHost tabHost;
	private RadioGroup tabGroup;
	Button menu;
	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		if(!LoginUtility.checkLoginStatus(this)){
			Intent i = new Intent(My_Profile.this,Login.class);
			startActivityForResult(i, 2);
		}
		else{
			setContentView(R.layout.my_profile);
	
			// Resources res = getResources(); // Resource object to get Drawables
			tabHost = getTabHost(); // The activity TabHost
			tabGroup = (RadioGroup) findViewById(R.id.tabGroup_My_Profile);
			
			TabHost.TabSpec spec; // Resusable TabSpec for each tab
			Intent intent; // Reusable Intent for each tab
	
			// Create an Intent to launch an Activity for the tab (to be reused)
			intent = new Intent().setClass(this, My_Profile_Account.class);
			spec = tabHost.newTabSpec("tab1").setIndicator("Account")
					.setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			tabHost.addTab(spec);
	
			intent = new Intent(this, My_Profile_Buy.class);
			spec = tabHost.newTabSpec("tab2").setIndicator("Buy/Borrow History").setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			tabHost.addTab(spec);
	
			intent = new Intent().setClass(this, My_Profile_Sell.class);
			spec = tabHost.newTabSpec("tab3").setIndicator("Sell/Lend History")
					.setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			tabHost.addTab(spec);
	
			intent = new Intent().setClass(this, My_Profile_Wishlist.class);
			spec = tabHost.newTabSpec("tab4").setIndicator("Wishlist")
					.setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			tabHost.addTab(spec);
			
			/*
			setTabColor( tabHost );
			tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
				public void onTabChanged(String tabId) {
					setTabColor(getTabHost());
				}
			
			});
			*/
			tabGroup.setOnCheckedChangeListener(new OnTabChangeListener());
			
			menu = (Button) findViewById(R.id.profile_menu);
			menu.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					 String[] choices={"Home","Help","Cancel"};  
					             AlertDialog dialog = new AlertDialog.Builder(My_Profile.this)  
					                     // .setIcon(android.R.drawable.btn_star)  
					                      .setTitle("Menu")  
					                      .setItems(choices, onselect_logged).create();  
					             dialog.show();  
				}
			});
		}
		
	}
	
	OnClickListener onselect_logged = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			Intent myIntent;
			switch (which) {
			case 0:
				myIntent = new Intent(My_Profile.this, Home.class);
				startActivity(myIntent);
				break;
			case 1:
				myIntent = new Intent(My_Profile.this, Help.class);
				startActivity(myIntent);
				break;
			case 2:
				dialog.dismiss();
				break;
			}
		}
	};
	
	private class OnTabChangeListener implements OnCheckedChangeListener{

		public void onCheckedChanged(RadioGroup arg0, int id) {
			// TODO Auto-generated method stub
			switch (id) {
            case R.id.tab1:
                tabHost.setCurrentTabByTag("tab1");
                break;
            case R.id.tab2:
                tabHost.setCurrentTabByTag("tab2");
                break;
            case R.id.tab3:
                tabHost.setCurrentTabByTag("tab3");
                break;
            case R.id.tab4:
            	tabHost.setCurrentTabByTag("tab4");
            	break;
 
            }
		}
		
	}
	public static void setTabColor(TabHost tabhost) {
		for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
			tabhost.getTabWidget().getChildAt(i)
					.setBackgroundColor(android.graphics.Color.WHITE); // unselected
			TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i)
					.findViewById(android.R.id.title);
			tv.setTextColor(android.graphics.Color.GREEN);
		}
		
		tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
				.setBackgroundColor(android.graphics.Color.GREEN); // selected
		
		TextView tv = (TextView) tabhost.getTabWidget()
				.getChildAt(tabhost.getCurrentTab())
				.findViewById(android.R.id.title);
		
		tv.setTextColor(android.graphics.Color.WHITE);
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        switch (requestCode) {  
            case 2:  
            	Intent intent = getIntent();
                overridePendingTransition(0, 0);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                finish();

                overridePendingTransition(0, 0);
                startActivity(intent);  
                break;  
            
        }  
    }
	

}
